package com.xh.bussiness.textbook.controller;

import com.alibaba.fastjson.JSONObject;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookModuleTree;
import com.xh.bussiness.textbook.bean.request.AddTextbookRequest;
import com.xh.bussiness.textbook.bean.request.QueryTextbookRequest;
import com.xh.bussiness.textbook.model.Textbook;
import com.xh.bussiness.textbook.service.ITextbookModuleService;
import com.xh.bussiness.textbook.service.ITextbookService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.CacheUtils;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲义相关
 * </p>
 *
 * @author Pitt zhang
 * @since 2019/9/26
 */
@Api(tags = "教案管理接口")
@RestController
@RequestMapping("/textbook")
public class TextbookController extends BaseController {

    @Autowired
    private ITextbookService textbookService;
    @Autowired
    private ITextbookModuleService textbookModuleService;

    @ApiOperation(value = "新增讲义", notes = "新增讲义")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "讲义信息", required = true, dataType = "AddTextbookRequest")
    })
    @PostMapping("/add")
    @Transactional
    @ResponseBody
    public AjaxResult addTextbook(@RequestBody AddTextbookRequest request) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,null, textbookService.addTextbook(request));
    }

    @ApiOperation(value = "分页查询讲义", notes = "分页查询讲义")
    @GetMapping("/queryPage")
    @ResponseBody
    public AjaxResult queryPage(XhPage page, QueryTextbookRequest request) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, textbookService.queryTextbookPage(page, request));
    }

    @ApiOperation(value = "根据id查询讲义详情", notes = "根据id查询讲义详情")
    @GetMapping("/queryTextbookById")
    @ResponseBody
    public AjaxResult queryTextbookById(String id) {
        if (StringUtils.isEmpty(id)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, textbookService.getTeachingBookById(id));
    }

    @ApiOperation(value = "删除讲义", notes = "删除讲义")
    @ApiImplicitParam(name = "id", value = "讲义ID", required = true, dataType = "String")
    @RequestMapping("/deleteTextbookById")
    public AjaxResult deleteTextbookById(String id) {
        if (StringUtils.isEmpty(id)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        boolean result = textbookService.delete(id);
        return new AjaxResult(result, result ? "删除成功" : "删除失败");
    }

    @ApiOperation(value = "讲义审核", notes = "讲义审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "讲义ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态(0：未提交，1：待审核，2：已通过，3：未通过)", required = true, dataType = "Integer"),
    })
    @RequestMapping("/auditTextbookById")
    public AjaxResult auditTextbookById(String id, Integer auditStatus) {
        if (StringUtils.isEmpty(id)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        Textbook textbook = textbookService.getById(id);
        if (textbook == null) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "讲义不存在");
        }
        JSONObject currentUser = (JSONObject) CacheUtils.get("user_" + UserUtils.getCurrentUserId());
        if (currentUser != null && auditStatus.equals(2)) {
            textbook.setAuditorId(currentUser.getString("id"));
            textbook.setAuditorName(currentUser.getString("username"));
        }
        //提交
        if (auditStatus.equals(1)) {
            textbook.setSubmitDate(new Date());
        }
        textbook.setAuditStatus(auditStatus);
        boolean update = textbookService.update(textbook);
        return new AjaxResult(update, update ? "审核成功" : "审核失败");
    }

    @ApiOperation(value = "获取讲义结构", notes = "获取讲义结构")
    @RequestMapping("/queryTextBookModule")
    public AjaxResult queryTextBookModule(String teachingBookId) {
       List<TeachingBookModuleTree> trees =
                textbookModuleService.queryTextBookModuleList(teachingBookId);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, trees);
    }
}
